﻿#include "SystemHelper.h"
#include "FileUtil.h"

SystemHelper::SystemHelper()
{
}


SystemHelper::~SystemHelper()
{
}

std::string SystemHelper::GetLocalIp()
{
	std::string ip = "127.0.0.1";
	boost::asio::io_service io_service;
	boost::asio::ip::tcp::resolver resolver(io_service);
	boost::asio::ip::tcp::resolver::query query(boost::asio::ip::host_name(), "");
	boost::asio::ip::tcp::resolver::iterator iter = resolver.resolve(query);
	boost::asio::ip::tcp::resolver::iterator end;
	while (iter != end) {
		boost::asio::ip::tcp::endpoint ep = *iter++;
		if (!ep.address().is_loopback()) {
			boost::asio::ip::address addr = ep.address();
			if (addr.is_v6())
			{
				continue;
			}
			ip = addr.to_string();
			break;
		}
	}
	return ip;
}

std::string SystemHelper::GetMachineName()
{
	boost::asio::io_service io_service;
	boost::asio::ip::tcp::resolver resolver(io_service);
	boost::asio::ip::tcp::resolver::query query(boost::asio::ip::host_name(), "");
	boost::asio::ip::tcp::resolver::iterator iter = resolver.resolve(query);
	boost::asio::ip::tcp::resolver::iterator end; // End marker. 
	while (iter != end)
	{
		boost::asio::ip::tcp::endpoint ep = *iter++;
		return ep.address().to_string();
	}
	return "";
}

bool SystemHelper::CanConnected(std::string ip, int port, int timeout_second)
{
	boost::asio::io_service ios;
	boost::asio::ip::tcp::socket sc(ios);
	sc.open(boost::asio::ip::tcp::v4());

	// 设为非阻塞
	boost::asio::ip::tcp::socket::non_blocking_io nb_option(true);
	sc.io_control(nb_option);

	// 连接服务器
	boost::system::error_code ec;
	sc.connect(boost::asio::ip::tcp::endpoint(boost::asio::ip::address::from_string(ip), port), ec);

	fd_set fdWrite;
	FD_ZERO(&fdWrite);
	FD_SET(sc.native(), &fdWrite);

	// 5秒超时
	timeval tv = { timeout_second };
	if (select(0, NULL, &fdWrite, NULL, &tv) <= 0 || !FD_ISSET(sc.native(), &fdWrite))
	{
		sc.close();
		return false;
	}

	// 设回阻塞
	boost::asio::ip::tcp::socket::non_blocking_io block_option(false);
	sc.io_control(block_option);
	sc.close();
	return true;
}

bool SystemHelper::IsPortOpen(const std::string& ip, const int& port) 
{
	using boost::asio::ip::tcp;
	boost::system::error_code ec;
	boost::asio::io_service svc;

	tcp::resolver resolver(svc);
	tcp::resolver::query query(ip, std::to_string(port));
	tcp::resolver::iterator endpoint_iterator = resolver.resolve(query, ec);
	if (ec) {
		return false;
	}

	tcp::socket socket(svc);
	boost::asio::connect(socket, endpoint_iterator, ec);
	return !ec;
}

std::string SystemHelper::GetDeviceImageDir(std::string device)
{
	std::string dir = FileUtil::GetAppPath(device) + "/images/";
	FileUtil::Mkdirs(dir);
	return dir;
}

std::string SystemHelper::GetDeviceImagePath(std::string device, std::string uuid)
{
	return GetDeviceImageDir(device) + uuid + ".jpg";
}

std::string SystemHelper::GetDeviceRecordDir(std::string device)
{
	std::string dir = FileUtil::GetAppPath(device) + "/records/";
	FileUtil::Mkdirs(dir);
	return dir;
}

std::string SystemHelper::GetDeviceRecordPath(std::string device, std::string uuid)
{
	return GetDeviceRecordDir(device) + uuid + ".mp4";
}

std::string SystemHelper::GetFileSavePath(std::string fileName)
{
	std::string path = FileUtil::GetAppPath("file/");
	FileUtil::Mkdirs(path);
	path += fileName;
	return path;
}

void SystemHelper::Reboot()
{
	// 重启AI盒子
#ifdef WIN32
	system("shutdown /r /t 0");
#else
	system("sudo /sbin/shutdown -r now");
#endif
}